#!/bin/bash

# Call this script after editing configure.ac or Makefile.am

# Copyright (C) 2005 Joe Friedrichsen <friedrij@users.berlios.de>
# 
# This program is free software; you can redistribute it and/or 
# modify it under the terms of the GNU General Public License 
# as published by the Free Software Foundation; either 
# version 2 of the License, or (at your option) any later 
# version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Test for good automake -- the 'no AM_PYTHON' error
# Minimum = 1.9.x
OLD_AUTOHELL=false
AUTOHELL_MAJOR=1
AUTOHELL_MINOR=9

if ! ( type aclocal >> /dev/null 2>&1 && type autoconf >> /dev/null 2>&1); then
    echo "You don't have the autotools installed (automake, autoconf, aclocal)."
    echo "You cannot bootstrap without them. Please install automake/aclocal 1.9"
    echo "or later and autoconf 2.5 or later and try again. Thanks!"
    exit 1
fi

AUTOHELL_VERSION=$(aclocal --version | head -n 1 | sed 's/[a-zA-Z ()]//g')

if test $(echo $AUTOHELL_VERSION | awk -F '.' '{print $1}') -lt $AUTOHELL_MAJOR;
then
    OLD_AUTOHELL=:
fi

if test $(echo $AUTOHELL_VERSION | awk -F '.' '{print $2}') -lt $AUTOHELL_MINOR;
then
    OLD_AUTOHELL=:
fi

if $OLD_AUTOHELL; then
    echo "Your version of automake is too old, you need at least automake $AUTOHELL_MAJOR.$AUTOHELL_MINOR"
    echo "to boostrap. Please install a newer automake and try again. Thanks!"
    echo "Exiting..."
fi

# See if tovid has been installed previously
if test -e Makefile; then
    echo "I found a Makefile! This may mean you have a previous version"
    echo "of tovid installed. Old tovid installations can cause strange"
    echo "and frustrating behavior. Do you want to uninstall it before"
    echo "continuing?"
    echo 
    echo -n "  Uninstall tovid? [y/N] "
    
    ANSWER=n
    read ANSWER
    
    if test "x$ANSWER" == "xy" || test "x$ANSWER" == "xY"; then
        echo -n "  Enter your root "
        su -c "make uninstall"
    fi      
    
    echo
    make distclean
fi

# Clean out old autotools-genreated files
rm -rvf "autom4te.cache"
rm -rvf aclocal.m4 config.log config.status configure Makefile Makefile.in

if aclocal \
   && automake --gnu --add-missing \
   && autoconf
then
   echo
   echo "Done. You may now do ./configure && su -c \"make install\""
   echo
else
   echo "Uf. Autotools error. Read above for clues."
   exit 1
fi

exit 0

